//
//  EnsureDelete.swift
//  zhonglb_iOS
//
//  Created by lxm on 2022/7/14.
//

import UIKit

class EnsureDelete: ZMViewController {

    //MARK:--------------------属性--------------------
    
    //MARK:--------------------UI控件--------------------
    @IBOutlet weak var codeText: UITextField!
    
    @IBOutlet weak var phoneTips: UILabel!
    
    @IBOutlet weak var codeBtn: UIButton!
    //MARK:--------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
    }
    //MARK:--------------------UI--------------------
    func setUI() {
        title = "注销账号"
        codeText.delegate = InputMananger.shard.onlyNumber
        guard let user = ZMAppShareData.share.userInfo.value else {return}
        phoneTips.text = "将\(user.phone.zm.hidPhone())所绑定的账号注销"
    }
    
    func addConstraints() {
        
    }
    
    //MARK:--------------------methods--------------------
    func bindAction() {
        
    }

    @IBAction func onCode(_ sender: UIButton) {
        guard let user = ZMAppShareData.share.userInfo.value else {return}
        ZMAPIManage<Any>().sendReqeust(method: LoginAPI.sendCode(["phone" : user.phone])) { (resp) in
            if resp.code == .success {
                ZMToast.hiddenActivity()
                sender.zm.countDown(count: 60)
                self.codeText.becomeFirstResponder()
            }
        }
    }
    
    @IBAction func onSure(_ sender: Any) {
        if codeText.text!.zm.isEmpty() {
            ZMToast.showToast(title: "请输入验证码")
            return
        }
        if codeText.text!.count != 4 {
            ZMToast.showToast(title: "验证码位数不正确")
            return
        }
        let config = AlertConfig()
        config.textStr = "提示"
        config.titleStr = "是否要删除当前账号?"
        config.cancelBtnText = "否"
        config.sureBtnText = "仍要删除"
        ZMAlertView.alert(config: config) { [unowned self] (index) in
            if index >= 0 {
                self.onSureDelete()
            }
        }
    }

    
    func onSureDelete() {
        ZMToast.showActivity()
        MineAPI.modifyPhone(["captcha":codeText.text!,"phone":"18380446471"]).rxRequestNullAble(type: Bool.self).subscribe(onNext:{ [unowned self] resp in
            ZMToast.hiddenActivity()
            if let data = resp, data {
                ZMToast.showToast(title: "已删除该账号")
                ZMAppShareData.share.clearUserInfo()
                self.navigationController?.popToRootViewController(animated: true)
            }
        }).disposed(by: self.disposeBag)
    }
    
    //MARK:--------------------delegate--------------------

}
